package com.governtalk.backend.controller;

import com.governtalk.backend.dto.forum.CategoryDto;
import com.governtalk.backend.dto.forum.PostDto;
import com.governtalk.backend.dto.forum.CommentDto;
import com.governtalk.backend.service.ForumService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/forum")
public class ForumController {
    
    @Autowired
    private ForumService forumService;
    
    @GetMapping("/categories")
    public ResponseEntity<List<CategoryDto>> getCategories() {
        List<CategoryDto> categories = forumService.getCategories();
        return ResponseEntity.ok(categories);
    }
    
    @GetMapping("/posts")
    public ResponseEntity<Page<PostDto>> getPosts(
            @RequestParam(required = false) Long categoryId,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) String sortBy,
            @RequestParam(required = false) String status,
            Pageable pageable) {
        Page<PostDto> posts = forumService.getPosts(categoryId, keyword, sortBy, status, pageable);
        return ResponseEntity.ok(posts);
    }
    
    @GetMapping("/posts/hot")
    public ResponseEntity<List<PostDto>> getHotPosts(@RequestParam(defaultValue = "10") int count) {
        List<PostDto> posts = forumService.getHotPosts(count);
        return ResponseEntity.ok(posts);
    }
    
    @GetMapping("/posts/latest")
    public ResponseEntity<List<PostDto>> getLatestPosts(@RequestParam(defaultValue = "10") int count) {
        List<PostDto> posts = forumService.getLatestPosts(count);
        return ResponseEntity.ok(posts);
    }
    
    @GetMapping("/posts/{postId}")
    public ResponseEntity<PostDto> getPostDetail(@PathVariable Long postId) {
        PostDto post = forumService.getPostDetail(postId);
        return ResponseEntity.ok(post);
    }
    
    @PostMapping("/posts")
    public ResponseEntity<PostDto> createPost(@RequestBody PostDto postDto) {
        PostDto post = forumService.createPost(postDto);
        return ResponseEntity.ok(post);
    }
    
    @PutMapping("/posts/{postId}")
    public ResponseEntity<PostDto> updatePost(
            @PathVariable Long postId,
            @RequestBody PostDto postDto) {
        PostDto post = forumService.updatePost(postId, postDto);
        return ResponseEntity.ok(post);
    }
    
    @DeleteMapping("/posts/{postId}")
    public ResponseEntity<String> deletePost(@PathVariable Long postId) {
        forumService.deletePost(postId);
        return ResponseEntity.ok("Post deleted successfully");
    }
    
    @PostMapping("/posts/{postId}/like")
    public ResponseEntity<Map<String, Object>> likePost(@PathVariable Long postId) {
        Map<String, Object> result = forumService.likePost(postId);
        return ResponseEntity.ok(result);
    }
    
    @GetMapping("/posts/{postId}/comments")
    public ResponseEntity<Page<CommentDto>> getPostComments(
            @PathVariable Long postId,
            @RequestParam(required = false) Long parentId,
            Pageable pageable) {
        Page<CommentDto> comments = forumService.getPostComments(postId, parentId, pageable);
        return ResponseEntity.ok(comments);
    }
    
    @PostMapping("/comments")
    public ResponseEntity<CommentDto> createComment(@RequestBody CommentDto commentDto) {
        CommentDto comment = forumService.createComment(commentDto);
        return ResponseEntity.ok(comment);
    }
    
    @DeleteMapping("/comments/{commentId}")
    public ResponseEntity<String> deleteComment(@PathVariable Long commentId) {
        forumService.deleteComment(commentId);
        return ResponseEntity.ok("Comment deleted successfully");
    }
    
    @PostMapping("/comments/{commentId}/like")
    public ResponseEntity<Map<String, Object>> likeComment(@PathVariable Long commentId) {
        Map<String, Object> result = forumService.likeComment(commentId);
        return ResponseEntity.ok(result);
    }
}
